package com.alkaid.search.serice;

import com.alibaba.fastjson.JSON;
import com.alkaid.commons.entity.Anime;
import com.alkaid.commons.ex.AnimeNotFoundException;
import com.alkaid.commons.ex.UnknownErrorException;
import com.alkaid.commons.param.anime.AnimePlay;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Kylin
 */
@Service
public class SearchServiceImpl implements SearchService {


    private RestHighLevelClient client;

    @Autowired
    public void setClient(RestHighLevelClient client) {
        this.client = client;
    }

    @Override
    public List<AnimePlay> searchAnime(String query) throws UnknownErrorException, AnimeNotFoundException {
        List<AnimePlay> animePlays = new ArrayList<>();
        SearchRequest searchRequest = new SearchRequest("alkaid-anime");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("anime.name", query));
        // 按照评分降序排列
        searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        String pre = "<span style=\"color: #75a9ff;\">";
        String postTags = "</span>";
        highlightBuilder.preTags(pre);
        highlightBuilder.postTags(postTags);
        highlightBuilder.highlighterType("unified");
        highlightBuilder.field("anime.name");
        searchSourceBuilder.highlighter(highlightBuilder);

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse;
        try {
            searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new UnknownErrorException("搜索失败");
        }
        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits.getHits()) {
            AnimePlay animePlay = JSON.parseObject(hit.getSourceAsString(), AnimePlay.class);
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField highlight = highlightFields.get("anime.name");
            Text[] fragments = highlight.fragments();
            String fragmentString = fragments[0].string();
            Anime anime = animePlay.getAnime();
            anime.setName(fragmentString);
            animePlay.setAnime(anime);
            animePlays.add(animePlay);
        }
        if (animePlays.size() == 0) {
            throw new AnimeNotFoundException("没有数据");
        }
        return animePlays;
    }
}
